
#import "HistoricalOrderListView.h"
#import "Masonry.h"
#import "HistoryOrderCell.h"
#import "OrderGoodsDetailController.h"
#import "HistoricalOrderController.h"
@interface HistoricalOrderListView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,assign) NSInteger type;
@property (nonatomic,assign) NSInteger index;
@property (nonatomic,strong) NSMutableArray *ordersArr;
@property (nonatomic,strong) UIView *placeholderView;
@property (nonatomic,weak)  UITableView *tableView;
@end
@implementation HistoricalOrderListView

- (instancetype) initWithFrame:(CGRect)frame type:(NSInteger)type viewController:(UIViewController*)viewController{
    self       = [super initWithFrame:frame];
    _type      = type;
    _VC        = viewController;
    _index     = 1;
    _ordersArr = [NSMutableArray array];
    [self setupUI];
    [self loadDataForWebWithType:_type];
    return self;
}
- (void) setupUI {
    self.backgroundColor = UIColorFromRGB(0xededed);
    CGFloat tableViewTopMargin = -48;
    CGFloat tableViewBottomMargin = 0;
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate   = self;
    tableView.backgroundColor = UIColorFromRGB(0xededed);
//    tableView.rowHeight = 80;
    [self addSubview:_tableView = tableView];
   // _tableView.backgroundColor = [UIColor redColor];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(tableViewTopMargin);
        make.bottom.equalTo(self).offset(-tableViewBottomMargin);
    }];
    
    __weak typeof(self) weakSelf = self;


    [self.tableView registerNib: [UINib nibWithNibName: @"HistoryOrderCell" bundle: nil] forCellReuseIdentifier: @"historyOrderCell"];
    [self addSubview:self.placeholderView];
    self.placeholderView.hidden = YES;
}
- (void) setupSplitLineWithTopMargin:(CGFloat)tableViewTopMargin bottomMargin:(CGFloat)tableViewBottomMargin{
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = RGB(212, 213, 214);
    [self addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.equalTo(@1);
        make.top.equalTo(self).offset(tableViewTopMargin-1);
    }];
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = RGB(212, 213, 214);
    [self addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.equalTo(@1);
        make.top.equalTo(self.mas_bottom).offset(-tableViewBottomMargin);
    }];
}

- (void) loadDataForWebWithType:(NSInteger)type{
    if(type ==3){
        self.type = 4;
    }else if(type ==5){
        self.type =3;
    }else if(type ==4){
        self.type =5;
    }
 
    
        JFBLoadingAnimation *animation = [[JFBLoadingAnimation alloc] init: self];
    
        [JFBSubmitOrderDS loadMenuList:^(NSArray *menuList, NSError *error) {
            [self.ordersArr removeAllObjects];
            [animation stop];
            [self.ordersArr addObjectsFromArray: menuList];
            [self.tableView reloadData];
            
        }andWithUT:[UserInfo currentUser].ut andWithMerchantId:[[UserInfo currentUser].merchantID integerValue] andWithPurchaseStatus:self.type andWithCurrentPage:1 andWithItemsPerPage:1000 andWithOrderItemNum:0];
    
  
}
- (UIView*) placeholderView{
    if (!_placeholderView) {
        _placeholderView = [[UIView alloc]initWithFrame:self.bounds];
        _placeholderView.backgroundColor = [UIColor whiteColor];
        
        UIImageView *noProduct = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth/2-30, 140, 60, 60)];
        noProduct.image = [UIImage imageNamed:@"iconfont-dingdan"];
        [_placeholderView addSubview:noProduct];
        
        UILabel *noText = [[UILabel alloc]initWithFrame:CGRectMake(0, 210, kScreenWidth, 25)];
        noText.text = @"您还没有相关订单";
        noText.textColor = UIColorFromRGB(0x999999);
        noText.font = [UIFont systemFontOfSize:15.0f];
        noText.textAlignment = NSTextAlignmentCenter;
        [_placeholderView addSubview:noText];
    }
    return _placeholderView;
}
#pragma mark - UITableView delegate
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    self.placeholderView.hidden = self.ordersArr.count?YES:NO;
    
    return 1;
}
#pragma mark - 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ordersArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"historyOrderCell";
    HistoryOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    HistoryOrderCell *cell = [tableView dequeueReusableCellWithIdentifier: identifier];
    if (!cell) {
        cell = [[HistoryOrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.historyDS = self.ordersArr[indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName: @"StoreManage" bundle: nil];
    OrderGoodsDetailController *orderDetailC = (OrderGoodsDetailController *)[storyBoard instantiateViewControllerWithIdentifier: @"orderGoodsDetailController"];
    JFBSubmitOrderDS *detail = self.ordersArr[indexPath.row];
    orderDetailC.orderDetailInfo = detail;
    
    [self.VC.navigationController pushViewController: orderDetailC animated: YES];

}












@end
